import asyncio
from util import delay, async_timed
from PyCmpltrtok.common import sep
import time


@async_timed()
async def call_later():
    print("I'm being called in the future!")
    await delay(2, 'call_later', 0.2)
    print('call_later done.')
    
    
def main():
    sep('Will get_running_loop')
    loop = asyncio.get_running_loop()
    
    """
    Traceback (most recent call last):
    File "/home/peiyp2004/code/python_basic/exercise/lib/asyncio/as_book/ch2_asyncio_basics/ch2_8_2_access_loop/listing_2_22_access_loop_x002000_no_loop_before_get.py", line 25, in <module>
        main()
    File "/home/peiyp2004/code/python_basic/exercise/lib/asyncio/as_book/ch2_asyncio_basics/ch2_8_2_access_loop/listing_2_22_access_loop_x002000_no_loop_before_get.py", line 16, in main
        loop = asyncio.get_running_loop()
    RuntimeError: no running event loop
    """
    
    print('Gotten running loop, will run_until_complete')
    loop.run_until_complete(call_later())
    print('run_until_complete invoked')
    print('Sleep 1')
    time.sleep(1)
    
    
if '__main__' == __name__:
    main()
    